package com.c.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.OrderSetting;
import com.c.entity.Permission;
import com.c.excel.OrderSettingData;
import com.c.excel.OrderSettingExcelListener;
import com.c.mapper.OrderSettingMapper;
import com.c.mapper.PermissionMapper;
import com.c.service.OrderSettingService;
import com.c.utils.MessageConstant;
import com.c.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

/**
 * @author wangxiangli
 * @create 2022-12-29-16:09
 */
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    /**
     * 根据月份查询对应的预约设置数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {//格式：yyyy-MM
        String begin = date + "-1";//2020-11-1
        String end="";
        int fastindex=date.indexOf("-");
        String str=date.substring(fastindex+1);
        int num=Integer.parseInt(str);
        if(num==1||num==3||num==5||num==7||num==8||num==10||num==12){
            end = date + "-31";//2020-12-31
        }else if (num==2){
            end = date + "-29";
        }else if(num==4||num==6||num==9||num==11){
            end = date + "-30";//2020-11-30
        }
        //String end = date + "-31";//2020-11-31
        HashMap<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        //调用DAO，根据日期范围查询预约设置数据
        List<OrderSetting> list = baseMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate());//获取日期数字（几号）
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    /**
     * 根据日期设置对应的预约设置数据
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        System.out.println("-----------"+orderDate.toString());
        //根据日期查询是否已经进行了预约设置
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("orderDate",orderDate);
        Long count = baseMapper.selectCount(wrapper);
        System.out.println("++++++++++++++++++++++++++"+count);
        if (count > 0) {
            //当前日期已经进行了预约设置，需要执行更新操作
            baseMapper.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有就那些预约设置，需要执行插入操作
            baseMapper.add(orderSetting);
        }
    }

    /**
     * 取消设置的预约
     *
     * @param date
     */
    @Override
    public Result cancelOrder(String date) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("reservations");
        wrapper.eq("orderDate",date);
         baseMapper.selectList(wrapper);
        OrderSetting orderSetting = baseMapper.selectOne(wrapper);
        long count=orderSetting.getReservations();
        if (count > 0) {
            //当前日期已经进行了预约设置,无法删除
            return new Result(false, MessageConstant.CANCELS_ORDERSETTING_FAIL);
        } else {
            //当前日期没有就那些预约设置，可以删除
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("orderDate",date);
            baseMapper.delete(wrapper1);
            return new Result(true, MessageConstant.CANCELS_ORDERSETTING_SUCCESS);
        }
    }

    @Override
    public void BatchaddOrderSettings(MultipartFile file, OrderSettingService orderSettingService) {
        try{
            //文件输入流
            InputStream in=file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, OrderSettingData.class,new OrderSettingExcelListener(orderSettingService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
